<?php

namespace App\Http\Controllers\Article\Web;

use App\Http\Controllers\BaseController;
use App\Http\Validate\Article\ArticleValidate;
use App\Service\Article\ArticleService;
use Exception;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class ArticleController extends BaseController
{
    /**
     * 添加文章/视频
     *
     * @return JsonResponse
     * @throws ValidationException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function add(): JsonResponse
    {
        ArticleService::add(ArticleValidate::addValidate());
        return $this->success();
    }

    /**
     * 更新文章/视频
     *
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function update(Request $request, int $id): JsonResponse
    {
        ArticleService::update(ArticleValidate::updateValidate($request->merge(["id" => $id])->all()));
        return $this->success();
    }

    /**
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function sort(Request $request, int $id): JsonResponse
    {
        ArticleService::sort(ArticleValidate::sortValidate($request->merge(["id" => $id])->all()));
        return $this->success();
    }

    /**
     * 置顶接口
     *
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function putTop(Request $request, int $id): JsonResponse
    {
        ArticleService::putTop(ArticleValidate::putTopValidate($request->merge(["id" => $id])->all()));
        return $this->success();
    }

    /**
     * 内容详情
     *
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function detail(Request $request, int $id): JsonResponse
    {
        ArticleValidate::detailValidate($request->merge(["id" => $id])->all());
        return $this->success(ArticleService::detail($id));
    }

    /**
     * 删除 文章|视频
     *
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function del(Request $request, int $id): JsonResponse
    {
        ArticleValidate::delValidate($request->merge(["id" => $id])->all());
        ArticleService::del($id);
        return $this->success();
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function list(Request $request): JsonResponse
    {
        $params = ArticleValidate::listValidate($request->all());
        return $this->success(ArticleService::articleList($params));
    }

    /**
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function status(Request $request, int $id): JsonResponse
    {
        $params = ArticleValidate::statusValidate($request->merge(["id" => $id])->all());
        ArticleService::status($params);
        return $this->success();
    }

    /**
     * 置顶接口
     *
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     * @throws Exception
     */
    public function putHotTop(Request $request, int $id): JsonResponse
    {
        ArticleService::putHotTop(ArticleValidate::putHotTopValidate($request->merge(["id" => $id])->all()));
        return $this->success();
    }
}
